let util = require('../../../../../common/util/util')
const app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    week:'',
    day:'',
    value:'',
    xlist:[],
    final_baseline:0,
    xdate:'',
    timelist:[],
    remark:'',
    index:'',
    istoday:false,
    final:false,
    value:'',
    value1:'',
    value2:'',
    is_generate:false
  },
  onLoad(options) {
      let that =this;
      that.setData({
        week:options.week,
        day:options.day,
        treatment_item_id:options.treatment_item_id,
        index:options.index,
        xdate:util.formatDate_chinese_monthdate(new Date())
      })
  },
  xChildReload: function () {
    var pages = getCurrentPages();//当前页面栈
    if (pages.length > 1) {
      var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
      beforePage.xInfo();//触发父页面中的方法
    }
  },
  onReady() {

  },
  onShow() {
    let that=this
    that.xInit()
  },
  bindinput( e ){
      this.setData({
        value:e.detail.value
      })
  },
  bindinput1( e ){
    this.setData({
      value1:e.detail.value
    })
  },
  bindinput2( e ){
    this.setData({
      value2:e.detail.value
    })
  },
  // 获取基线数据
  xInit(){
      let that =this;
      wx.cloud.callFunction({
        name: 'digital_therapy',
        data: {
            action: "get_pain_baseline"
        }
      }).then(res => {
          var obj=[]
          for(let index in res.result.list){
            res.result.list[index].create_time = util.formatDate_chinese_monthdate(new Date(res.result.list[index].create_time))
          }
          that.setData({
            xlist:res.result.list
          })
          if(res.result.is_generate==true){
            that.setData({
              final_baseline:res.result.final_baseline,
              remark:'你的疼痛基线是'+ res.result.final_baseline +'分钟，当你在进行日常活动而感到疼痛时，在16分钟以内是安全的，可以尝试坚持下去',
              is_generate:res.result.is_generate
            })
          }
      })
  },
  // 删除某条
  xTodel(e){
    let that = this;
    let i = e.currentTarget.dataset.index
    wx.cloud.callFunction({
      name: 'digital_therapy',
      data: {
          action: "delete_pain_baseline",
          _id:that.data.xlist[i]._id//当前疼痛基线单个数据的_id值
      }
    }).then(res => {
        that.xInit()
    })
  },
  // 保存用户基线
  xTosave(e){
    let that =this;
    let index  = e.currentTarget.dataset.index
    let val ='';
    if(index==0){
      val= Number(that.data.value)
    }else if(index==1){
      val= Number(that.data.value1)
    }else{
      val= Number(that.data.value2)
    }
    let _dataObj = {
      week:Number(that.data.week),
      day:Number(that.data.day),
      baseline:val,
      treatment_item_id:that.data.treatment_item_id
    }
    wx.cloud.callFunction({
        name: 'digital_therapy',
        data: {
            action: "save_pain_baseline",
            dataObj:_dataObj
        }
    }).then(res => {
        // app.globalData.treatment_item_content[that.data.index]=true
        that.setData({
          value:''
        })
        that.xInit()
        that.xChildReload()
    })
  },
  setbaseline_true:function(){
    let that = this;
    wx.cloud.callFunction({
      name: 'digital_therapy',
        data: {
            action: "generate_pain_baseline"
        }
    }).then(res => {
      that.xInit()
    })
  },
  onHide() {
     
  },
  onUnload() {

  },
  onPullDownRefresh() {

  },
  onReachBottom() {

  },
  onShareAppMessage() {

  }
})